package cn.circle.system.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.commom.model.PageHelper;
import cn.circle.system.dao.ISysCustomerDao;
import cn.circle.system.model.SysCustomer;

@Repository
public class SysCustomerDao extends BaseDaoImpl<SysCustomer> implements ISysCustomerDao {
	
/*	@Autowired
	private ISysCustomerDao sysCustomerDao;*/
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SysCustomer> datagrid() {
		// TODO Auto-generated method stub
		String hql = "from SysCustomer ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@Override
	public List<SysCustomer> getUser(String id) {
		// TODO Auto-generated method stub
		
		String hql = "from SysCustomer where custId = '"+id+"'";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@Override
	public boolean deleteAll(String ids) {
		String hql = "delete SysCustomer s where s.custId in ("+ids+") ";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		//query.setString(0, ids);
		return (query.executeUpdate() > 0);
	}
	public boolean updataAll(SysCustomer c){
		StringBuffer hql = new StringBuffer(
				"update SysCustomer s set ");
		
		//拼接修改条件
		if(c.getUserName() != null && !"".equals(c.getUserName())){
			hql.append("  s.userName = '"+c.getUserName()+"' ");
		}
//		if(c.getLoginName() != null && !"".equals(c.getLoginName())){
//			hql.append(" , s.loginName = '"+c.getLoginName()+"' ");
//		}
//
		if(c.getPassWord() != null && !"".equals(c.getPassWord())){
			hql.append(" , s.passWord = '"+c.getPassWord()+"' ");
		}
		if(c.getSysDepartment().getDepId() != null && !"".equals(c.getSysDepartment().getDepId())){
			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
		}
		if (c.getSysRoles() != null) {
			hql.append(" , s.sysRoles.id = " + c.getSysRoles().getId() + " ");
		}
//		if(c.getSysDepartment().getDepId() != null && !"".equals(c.getSysDepartment().getDepId())){
//			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
//		}
//		if(c.getSex() != null && !"".equals(c.getSex())){
//			hql.append(" , s.sex = "+c.getSex()+" ");
//		}
//		if(c.getEmail() != null ){
//			hql.append(" , s.email = '"+c.getEmail()+"' ");
//		}
//		if(c.getCustType() != null && !"".equals(c.getCustType())){
//			hql.append(" , s.custType = "+c.getCustType()+" ");
//		}
		if(c.getPhone() != null ){
			hql.append(" , s.phone = '"+c.getPhone()+"' ");
		}
		
		if(c.getStatus() != null && !"".equals(c.getStatus())){
			hql.append(" , s.status = "+c.getStatus()+" ");
		}

		hql.append(" where s.custId ="+c.getCustId()+" ");
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
	/*	return (sysCustomerDao.executeHql(hql.toString())) > 0;*/
		
	}

}
